package com.pioneer.web.archive.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pioneer.common.exception.CustomException;
import com.pioneer.common.utils.DictUtils;
import com.pioneer.common.utils.SecurityUtils;
import com.pioneer.web.archive.domain.ArcSupplier;
import com.pioneer.web.archive.mapper.ArcSupplierMapper;
import com.pioneer.web.archive.service.IArcSupplierService;
import com.pioneer.web.system.domain.SysDictData;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ArcSupplierServiceImpl
 *
 * @author hlm
 * @date 2025-03-20 14:19:26
 */
@Service
public class ArcSupplierServiceImpl extends ServiceImpl<ArcSupplierMapper, ArcSupplier> implements IArcSupplierService {

    @Override
    public boolean checkCodeUnique(ArcSupplier supplier) {
        LambdaQueryWrapper<ArcSupplier> query = Wrappers.lambdaQuery();
        query.eq(ArcSupplier::getSupplierCode, supplier.getSupplierCode());
        if (supplier.getSupplierId() != null) {
            query.ne(ArcSupplier::getSupplierId, supplier.getSupplierId());
        }
        return getBaseMapper().exists(query);
    }

    @Override
    public String importSupplier(List<ArcSupplier> supplierList) {
        StringBuilder msg = new StringBuilder();
        List<SysDictData> typeList = DictUtils.getDictCache("arc_supplier_type");
        if (CollUtil.isEmpty(typeList)) {
            throw new CustomException("供应商行业数据字典为空，请在[系统管理-字段管理]节点进行维护！");
        }
        List<SysDictData> tradeList = DictUtils.getDictCache("arc_supplier_trade");
        if (CollUtil.isEmpty(tradeList)) {
            throw new CustomException("所属行业数据字典为空，请在[系统管理-字段管理]节点进行维护！");
        }
        List<SysDictData> creditList = DictUtils.getDictCache("arc_supplier_credit");
        if (CollUtil.isEmpty(creditList)) {
            throw new CustomException("信用等级数据字典为空，请在[系统管理-字段管理]节点进行维护！");
        }

        for (int i = 0; i < supplierList.size(); i++) {
            ArcSupplier supplier = supplierList.get(i);
            typeList.stream().filter(type -> type.getDictLabel().equals(supplier.getType())).findFirst().ifPresent(type -> supplier.setType(type.getDictValue()));
            if (supplier.getType() == null) {
                msg.append("第").append(i + 2).append("行供应商类型不匹配，请重新检查");
            }
            tradeList.stream().filter(trade -> trade.getDictLabel().equals(supplier.getTrade())).findFirst().ifPresent(trade -> supplier.setTrade(trade.getDictValue()));
            if (supplier.getTrade() == null) {
                msg.append("第").append(i + 2).append("行所属行业不匹配，请重新检查");
            }
            creditList.stream().filter(credit -> credit.getDictLabel().equals(supplier.getCredit())).findFirst().ifPresent(credit -> supplier.setCredit(credit.getDictValue()));
            if (supplier.getType() == null) {
                msg.append("第").append(i + 2).append("行信用等级不匹配，请重新检查");
            }
            supplier.setCreateBy(SecurityUtils.getLoginUser().getUsername());
            supplier.setCreateTime(LocalDateTime.now());
        }
        // 批量新增
        int ret = getBaseMapper().insertBatch(supplierList);
        if (ret <= 0) {
            return "导入失败，请联系管理员！";
        }
        if (!msg.isEmpty()) {
            return msg.toString();
        }
        return "导入成功";
    }
}
